package net.zhengxinyun.unifyApp.ctrl;

import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.unifyApp.Util.SystemApiUtl;
import net.zhengxinyun.unifyApp.core.Result;
import net.zhengxinyun.unifyApp.core.ResultUtils;
import net.zhengxinyun.unifyApp.entity.VersionEntity;
import net.zhengxinyun.unifyApp.service.AppVersion.VersionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationHome;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/apkvsersion")
public class ApkVersionController  {

    protected Logger logger  = LoggerFactory.getLogger(ApkVersionController.class);

    @Autowired
    private  VersionService versionService;
    @ResponseBody
    @PostMapping("/update")
    public Result<HashMap<String,Object>> detailApkVersion(@RequestBody VersionEntity filter){
        String path="";

        if (StringUtils.isBlank(filter.getVersions())) {
            return  ResultUtils.getResults(Result.Status.ERROR,"versions参数为空",null);
        }

        VersionEntity vsersion = versionService.getVsersion(null); //获取最新的版本号
        if (! vsersion.getVersions().equals(filter.getVersions())){
        HashMap<String,Object> map =new HashMap<>();
            path = SystemApiUtl.SERVICEADDRESS+":8383" +
                    "/downApk/unifyApp.apk";
            map.put("URL",path);
            String updateInfo = vsersion.getUpdateInfo();

            map.put("updInfo",StringUtils.isBlank(updateInfo)?"": updateInfo.replace(";","\n"));
            return ResultUtils.getResults(Result.Status.SUCCESS,"检测到最新版，请更新",map);
        }




        return ResultUtils.getResults(Result.Status.ERROR,"已经是最新版本，无需更新",null);

    }

    /**
     * 获取所有版本号
     */
    @ResponseBody
    @PostMapping("/getAllVersion")
    public Result<HashMap<String,Object>> getAllVersion(@RequestBody VersionEntity filter){


       // versionService.getVsergetion()
        List<VersionEntity> allVersion = versionService.getAllVersion();
        HashMap<String,Object>  map = new HashMap<>();
        map.put("data",allVersion);

        return ResultUtils.getSuccessResults(map);
    }

}
